import { http } from "../http";
import {
  mockGetContractList,
  mockAddOrUpdateContract,
  mockDeleteContract,
  mockGetContractById,
} from "../mock/contract";

const baseUrl = "/api/dms/maintenanceContracts";

// 控制是否使用Mock API的全局变量
const USE_MOCK = false; // 设置为 true 使用Mock，false 使用真实API

export interface ContractType {
  id: string;
  contractName: string;
  supplierName: string;
  deviceId: string;
  startDate: string;
  endDate: string;
}

export interface AddOrUpdateContractQueryType {
  id?: string | null;
  contractName: string | null;
  supplierName: string | null;
  deviceId: string | null;
  startDate: number | null;
  endDate: number | null;
}

// 添加或编辑合同
export const addMaintenanceContractAPI = (
  params: AddOrUpdateContractQueryType
) => {
  if (USE_MOCK) {
    return mockAddOrUpdateContract(params).then((data) => data.data);
  }
  return http.post(`${baseUrl}/addOrUpdateMaintenanceContract`, params);
};

// 删除合同
export const deleteMaintenanceContractAPI = (id: string) => {
  if (USE_MOCK) {
    return mockDeleteContract(id).then((data) => data.data);
  }
  return http.delete(`${baseUrl}/deleteMaintenanceContract/${id}`);
};

// 根据ID获取合同详情
export const getMaintenanceContractsByDeviceIdAPI = (id: string) => {
  if (USE_MOCK) {
    return mockGetContractById(id).then((data) => data.data);
  }
  return http.get(`${baseUrl}/getMaintenanceContractsByDeviceId/${id}`);
};
